package start.spring.basic.flowable.service.flowable.impl;


import java.util.Objects;

import org.flowable.common.engine.api.FlowableObjectNotFoundException;
import org.flowable.engine.history.HistoricProcessInstance;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import start.spring.basic.flowable.service.FlowableBaseServiceImpl;
import start.spring.basic.flowable.service.flowable.IWfInstanceService;

/**
 * 工作流流程实例管理
 *
 * @author KonBAI
 * @createTime 2022/3/10 00:12
 */
@Service
public class WfInstanceServiceImpl extends FlowableBaseServiceImpl implements IWfInstanceService {

//	@Autowired
//    private IWfDeployFormService deployFormService;
	
//	@Autowired
//    private UserService userService;
//	
//	@Autowired
//    private ISysRoleService roleService;
//	
//	@Autowired
//    private ISysDeptService deptService;

//    /**
//     * 结束流程实例
//     *
//     * @param vo
//     */
//    @Override
//    public void stopProcessInstance(WfTaskBo vo) {
//        String taskId = vo.getTaskId();
//
//    }

    /**
     * 激活或挂起流程实例
     *
     * @param state      状态
     * @param instanceId 流程实例ID
     */
    @Override
    public void updateState(Integer state, String instanceId) {

        // 激活
        if (state == 1) {
            runtimeService.activateProcessInstanceById(instanceId);
        }
        // 挂起
        if (state == 2) {
            runtimeService.suspendProcessInstanceById(instanceId);
        }
    }

    /**
     * 删除流程实例ID
     *
     * @param instanceId   流程实例ID
     * @param deleteReason 删除原因
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String instanceId, String deleteReason) {

        // 查询历史数据
        HistoricProcessInstance historicProcessInstance = getHistoricProcessInstanceById(instanceId);
        if (historicProcessInstance.getEndTime() != null) {
            historyService.deleteHistoricProcessInstance(historicProcessInstance.getId());
            return;
        }
        // 删除流程实例
        runtimeService.deleteProcessInstance(instanceId, deleteReason);
        // 删除历史流程实例
        historyService.deleteHistoricProcessInstance(instanceId);
    }

    /**
     * 根据实例ID查询历史实例数据
     *
     * @param processInstanceId
     * @return
     */
    @Override
    public HistoricProcessInstance getHistoricProcessInstanceById(String processInstanceId) {
        HistoricProcessInstance historicProcessInstance =
                historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        if (Objects.isNull(historicProcessInstance)) {
            throw new FlowableObjectNotFoundException("流程实例不存在: " + processInstanceId);
        }
        return historicProcessInstance;
    }


//    /**
//     * 流程历史流转记录
//     *
//     * @param procInsId 流程实例Id
//     * @return
//     */
//    @Override
//    public Map<String, Object> queryDetailProcess(String procInsId, String deployId) {
//        Map<String, Object> map = new HashMap<>();
//        if (StringUtils.isNotBlank(procInsId)) {
//            List<HistoricTaskInstance> taskInstanceList = historyService.createHistoricTaskInstanceQuery()
//                .processInstanceId(procInsId)
//                .orderByHistoricTaskInstanceStartTime().desc()
//                .list();
//            List<Comment> commentList = taskService.getProcessInstanceComments(procInsId);
//            List<WfTaskVo> taskVoList = new ArrayList<>(taskInstanceList.size());
//            taskInstanceList.forEach(taskInstance -> {
//                WfTaskVo taskVo = new WfTaskVo();
//                taskVo.setProcDefId(taskInstance.getProcessDefinitionId());
//                taskVo.setTaskId(taskInstance.getId());
//                taskVo.setTaskDefKey(taskInstance.getTaskDefinitionKey());
//                taskVo.setTaskName(taskInstance.getName());
//                taskVo.setCreateTime(taskInstance.getStartTime());
//                taskVo.setFinishTime(taskInstance.getEndTime());
//                if (StringUtils.isNotBlank(taskInstance.getAssignee())) {
//                    Long userId = Long.parseLong(taskInstance.getAssignee());
//                    //TODO 补充获取用户信息内容
//                    String nickName = "";//userService.selectNickNameById(userId);
//                    taskVo.setAssigneeId(userId);
//                    taskVo.setAssigneeName(nickName);
//                }
//                // 展示审批人员
//                List<HistoricIdentityLink> linksForTask = historyService.getHistoricIdentityLinksForTask(taskInstance.getId());
//                StringBuilder stringBuilder = new StringBuilder();
//                for (HistoricIdentityLink identityLink : linksForTask) {
//                    if ("candidate".equals(identityLink.getType())) {
//                        if (StringUtils.isNotBlank(identityLink.getUserId())) {
//                            Long userId = Long.parseLong(identityLink.getUserId());
//                            //TODO 补充获取用户信息内容
//                            String nickName = "";//userService.selectNickNameById(userId);
//                            stringBuilder.append(nickName).append(",");
//                        }
//                        if (StringUtils.isNotBlank(identityLink.getGroupId())) {
//                            if (identityLink.getGroupId().startsWith(TaskConstants.ROLE_GROUP_PREFIX)) {
//                                Long roleId = Long.parseLong(StringUtils.stripStart(identityLink.getGroupId(), TaskConstants.ROLE_GROUP_PREFIX));
//                                //TODO 补充获取用户角色信息内容
//                                SysRole role = null;//roleService.selectRoleById(roleId);
//                                stringBuilder.append(role.getRoleName()).append(",");
//                            } else if (identityLink.getGroupId().startsWith(TaskConstants.DEPT_GROUP_PREFIX)) {
//                                Long deptId = Long.parseLong(StringUtils.stripStart(identityLink.getGroupId(), TaskConstants.DEPT_GROUP_PREFIX));
//                                //TODO 补充获取用户部门信息内容
//                                SysDept dept = null;//deptService.selectDeptById(deptId);
//                                stringBuilder.append(dept.getDeptName()).append(",");
//                            }
//                        }
//                    }
//                }
//                if (StringUtils.isNotBlank(stringBuilder)) {
//                    taskVo.setCandidate(stringBuilder.substring(0, stringBuilder.length() - 1));
//                }
//                if (ObjectUtil.isNotNull(taskInstance.getDurationInMillis())) {
//                    taskVo.setDuration(DateUtil.formatBetween(taskInstance.getDurationInMillis(), BetweenFormatter.Level.SECOND));
//                }
//                // 获取意见评论内容
//                if (CollUtil.isNotEmpty(commentList)) {
//                    List<Comment> comments = new ArrayList<>();
//                    // commentList.stream().filter(comment -> taskInstance.getId().equals(comment.getTaskId())).collect(Collectors.toList());
//                    for (Comment comment : commentList) {
//                        if (comment.getTaskId().equals(taskInstance.getId())) {
//                            comments.add(comment);
//                            // taskVo.setComment(WfCommentDto.builder().type(comment.getType()).comment(comment.getFullMessage()).build());
//                        }
//                    }
//                    //TODO 补充获取获取评论内容
////                    taskVo.setCommentList(comments);
//                }
//                taskVoList.add(taskVo);
//            });
//            map.put("flowList", taskVoList);
//        }
//        return map;
//    }
}
